package com.bwie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bwie.es.EsVisitor;
import com.bwie.pojo.*;
import com.bwie.service.*;
import com.bwie.unit.R;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.print.attribute.standard.PageRanges;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class ListController {


    @Autowired
    private PeopleService peopleService;


    @Autowired
    private TeamService teamService;


    @Autowired
    private ReasonService reasonService;


    @Autowired
    private LinkageService linkageService;

    @Autowired
    private VisitorService visitorService;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @PostMapping("peopleList")
    public R peopleList(@RequestHeader(name = "token") String token){
        Claims body = Jwts.parser().setSigningKey("bw".getBytes()).parseClaimsJws(token).getBody();
        Integer uid = body.get("uid", Integer.class);
        Integer rid = body.get("rid", Integer.class);
        List<People> peopleList = peopleService.peopleList(uid,rid);
        return R.OK(peopleList);
    }

    @PostMapping("teamList")
    public R teamList(){
       List<Team> teamList = teamService.teamList();
       return R.OK(teamList);
    }

    @PostMapping("createEs")
    public R createEs(){
        return peopleService.createEs();
    }

    @PostMapping("EsList")
    public R EsList(@RequestBody Visitor visitor){
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if (!StringUtils.isEmpty(visitor.getVisitorName())){
            boolQueryBuilder.must(QueryBuilders.matchQuery("visitorName",visitor.getVisitorName()));
        }

        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("visitorName").preTags("<font color='red'>").postTags("</font>");

        int pageNum = 0;
        if (visitor.getPageNum()>0){
             pageNum = visitor.getPageNum()-1;
        }
        PageRequest of = PageRequest.of(pageNum, visitor.getPageSize());

        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withQuery(boolQueryBuilder)
                .withHighlightBuilder(highlightBuilder)
                .withPageable(of);

        ArrayList<EsVisitor> list = new ArrayList<>();
        SearchHits<EsVisitor> search = elasticsearchRestTemplate.search(nativeSearchQueryBuilder.build(), EsVisitor.class);
        long total = search.getTotalHits();

        for (SearchHit<EsVisitor> esVisitorSearchHit : search) {
            EsVisitor content = esVisitorSearchHit.getContent();
            List<String> visitorName = esVisitorSearchHit.getHighlightField("visitorName");
            if (visitorName!=null && visitorName.size()>0){
                String s = visitorName.get(0);
                content.setVisitorName(s);
            }
            list.add(content);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",visitor.getPageSize());
        map.put("total",total);
        map.put("list",list);
        return R.OK(map);
    }

    @PostMapping("reasonList")
    public R reasonList(){
        List<Reason> list = reasonService.list();
        return R.OK(list);
    }

    @PostMapping("getLinkageList/{id}")
    public R getLinkageList(@PathVariable Integer id){
        LambdaQueryWrapper<Linkage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Linkage::getLinkageId,id);
        List<Linkage> list = linkageService.list(wrapper);
        return R.OK(list);
    }

    @PostMapping("add")
    public R add(@RequestBody Visitor visitor){
        visitorService.save(visitor);
        return R.OK();
    }

}
